//
//  LLSettingVC.m
//  Diary
//
//  Created by lanou3g on 15/11/9.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "LLSettingVC.h"
#import "FZLDiaryDataObject.h"
#import "GHDiaryDataHandle.h"
#import "AppDelegate.h"
#define kCachePath  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

//#import <CoreData/CoreData.h>
@interface LLSettingVC ()

@property (nonatomic, retain)NSManagedObjectContext *manageContext;

@end

@implementation LLSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self addSubViews];
    
}

- (void)addSubViews{
    self.llSettingView = [[LLSettingView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.llSettingView];
    AVUser *user = [AVUser currentUser];
    if (user) {
        NSURL *imageUrl = [[user valueForKey:@"photo"] url];
        [self.llSettingView.photoImageView  sd_setImageWithURL:imageUrl placeholderImage:nil];
    }
    // 缓存
    self.llSettingView.cachesLabel.text = [NSString stringWithFormat:@"清除缓存(%.1fM)",[self folderSizeAtPath:kCachePath]];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionSwipe:)];
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipe];
    
    // 设置用户图像
    UITapGestureRecognizer *photoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionPhotoTap:)];
    [self.llSettingView.photoImageView addGestureRecognizer:photoTap];
    // 同步到云端
    UITapGestureRecognizer *syncTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionSyncTap:)];
    [self.llSettingView.syncLabel addGestureRecognizer:syncTap];
    // 清除缓存
    
    UITapGestureRecognizer *cachesTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionCachesTap:)];
    [self.llSettingView.cachesLabel addGestureRecognizer:cachesTap];

    // 反馈意见
    UITapGestureRecognizer *suggestTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionSuggestTap:)];
    [self.llSettingView.suggestLabel addGestureRecognizer:suggestTap];
    // 关于我们
    UITapGestureRecognizer *mineTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMineTap:)];
    [self.llSettingView.mineLabel addGestureRecognizer:mineTap];
    
    [self.llSettingView.loginOrRegisterButton addTarget:self action:@selector(actionLoginOrRegisterButton:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)actionSwipe:(UISwipeGestureRecognizer *)sender{
    if (
        [self.llSettingView.userTextField isFirstResponder] ||
        [self.llSettingView.passwordTextField isFirstResponder] ||
        [self.llSettingView.registerUserTextField isFirstResponder] ||
        [self.llSettingView.registerPasswordTextField isFirstResponder] ||
        [self.llSettingView.registerAgainPasswordTextField isFirstResponder]) {
        
        [self.llSettingView.userTextField resignFirstResponder];
        [self.llSettingView.passwordTextField resignFirstResponder];
        [self.llSettingView.registerUserTextField resignFirstResponder];
        [self.llSettingView.registerPasswordTextField resignFirstResponder];
        [self.llSettingView.registerAgainPasswordTextField resignFirstResponder];
        
    }else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)actionSyncTap:(UITapGestureRecognizer *)sender{
    GHDiaryDataHandle *diaryData = [[GHDiaryDataHandle alloc] init];
    NSMutableArray *diaryDataArray = [diaryData getModelArrayWithManagedObjectContext:self.manageContext];
    for (GHDiaryDataModel *dataModel in diaryDataArray) {
        AVQuery *query = [FZLDiaryDataObject query];
        NSArray *objects = [query findObjects:nil];
        if (objects.count == 0) {
            [self savaDataWithDiaryModel:dataModel];
        } else {
        for (FZLDiaryDataObject *object in objects) {
                if ([object.diaryID isEqualToString:dataModel.diaryID]) {
                    break;
                } else {
                    [self savaDataWithDiaryModel:dataModel];
                }
            }
    }
    }
    
}
- (void)savaDataWithDiaryModel:(GHDiaryDataModel *) dataModel
{
    {
        FZLDiaryDataObject *object = [[FZLDiaryDataObject alloc] init];
        object.diaryID = dataModel.diaryID;
        object.clickTag = dataModel.clickTag;
        //        if (![dataModel.photoImg isEqual:@""]) {
        //              NSData *photoImageData = UIImagePNGRepresentation(dataModel.photoImg);
        //              object.photoImg = [AVFile fileWithData:photoImageData];
        //        } else {
        //            object.photoImg = nil;
        //        }
        object.title = dataModel.title;
        object.subtitle = dataModel.subtitle;
        object.summary = dataModel.summary;
        object.dateLabelText = dataModel.dateLabelText;
        object.placeLabel = dataModel.placeLabel;
        //        NSData *diaryImageData = UIImagePNGRepresentation(dataModel.diaryImage);
        //        object.diaryImage = [AVFile fileWithData:diaryImageData];
        [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                //NSLog(@"保存成功");
            } else {
                //NSLog(@"%@",error);
            }
        }];
        
    }
}
- (void)actionCachesTap:(UITapGestureRecognizer *)sender{
    [self clearCache:kCachePath];
    float cacheSize = [self folderSizeAtPath:kCachePath];
   self.llSettingView.cachesLabel.text = [NSString stringWithFormat:@"清除缓存(%.2fM)",cacheSize];
}

- (void)actionSuggestTap:(UITapGestureRecognizer *)sender{
    //NSLog(@"反馈意见");
}
- (void)actionMineTap:(UITapGestureRecognizer *)sender{
    //NSLog(@"关于我们");
}
// 懒加载
- (NSManagedObjectContext *)manageContext
{
    if (!_manageContext) {
        _manageContext = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
    }
    return _manageContext;
}

// 计算目录大小
- (float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize += [[SDImageCache sharedImageCache] getSize] /1024.0/1024.0;
        return folderSize;
    }
    return 0;
}

// 计算单个文件
- (float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

// 清理缓存文件
- (void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            //            if ([fileName isEqualToString:@"com.Lanou.OrangeAmusement"]) {
            //                break;
            //            }
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}

#pragma mark --- 登陆界面
- (void)actionLoginOrRegisterButton:(UIButton *)sender{
    
    if (!self.llSettingView.loginBaseImageView) {
        [self addLoginView];
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.llSettingView.loginBaseImageView.frame = CGRectMake(CGRectGetMinX(self.llSettingView.photoImageView.frame), CGRectGetMinY(self.llSettingView.photoImageView.frame), CGRectGetWidth(self.llSettingView.photoImageView.frame), CGRectGetHeight(self.llSettingView.photoImageView.frame));
    }];
    
    
}
#pragma mark -- 登陆界面布局
- (void)addLoginView{
    // 登录界面
    self.llSettingView.loginBaseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.photoImageView.frame), -CGRectGetHeight(self.llSettingView.photoImageView.frame), CGRectGetWidth(self.llSettingView.photoImageView.frame), CGRectGetHeight(self.llSettingView.photoImageView.frame))];
    self.llSettingView.loginBaseImageView.userInteractionEnabled = YES;
    self.llSettingView.loginBaseImageView.image = [UIImage imageNamed:@"whitePaperBG"];
    [self.llSettingView addSubview:self.llSettingView.loginBaseImageView];
    
    self.llSettingView.userTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.llSettingView.photoImageView.frame)*0.1, CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.2, CGRectGetWidth(self.llSettingView.photoImageView.frame)*0.8, CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.12)];
    self.llSettingView.userTextField.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    
    UIImageView *userLeftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.llSettingView.userTextField.frame)*0.1, CGRectGetHeight(self.llSettingView.userTextField.frame)*0.8, CGRectGetHeight(self.llSettingView.userTextField.frame)*0.8)];
    userLeftView.image = [UIImage imageNamed:@"login_icon"];
    
    self.llSettingView.userTextField.leftView = userLeftView;
    self.llSettingView.userTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.llSettingView.loginBaseImageView addSubview:self.llSettingView.userTextField];
    UIView *lineUserView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.userTextField.frame), CGRectGetMaxY(self.llSettingView.userTextField.frame), CGRectGetWidth(self.llSettingView.userTextField.frame), 2)];
    lineUserView.backgroundColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    [self.llSettingView.loginBaseImageView addSubview:lineUserView];
    
    self.llSettingView.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.userTextField.frame), CGRectGetMaxY(self.llSettingView.userTextField.frame)+CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.02, CGRectGetWidth(self.llSettingView.userTextField.frame), CGRectGetHeight(self.llSettingView.userTextField.frame))];
    self.llSettingView.passwordTextField.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    self.llSettingView.passwordTextField.secureTextEntry = YES;
    
    UIImageView *passwordLeftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.llSettingView.passwordTextField.frame)*0.1, CGRectGetHeight(self.llSettingView.passwordTextField.frame)*0.8, CGRectGetHeight(self.llSettingView.passwordTextField.frame)*0.8)];
    passwordLeftView.image = [UIImage imageNamed:@"password_icon"];
    self.llSettingView.passwordTextField.leftView = passwordLeftView;
    self.llSettingView.passwordTextField.leftViewMode = UITextFieldViewModeAlways;
    
    [self.llSettingView.loginBaseImageView addSubview:self.llSettingView.passwordTextField];
    UIView *linePasswordView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.passwordTextField.frame), CGRectGetMaxY(self.llSettingView.passwordTextField.frame), CGRectGetWidth(self.llSettingView.passwordTextField.frame), 2)];
    linePasswordView.backgroundColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    [self.llSettingView.loginBaseImageView addSubview:linePasswordView];
    
    // 注册
    self.llSettingView.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.llSettingView.registerButton.frame = CGRectMake(CGRectGetMinX(self.llSettingView.passwordTextField.frame)+CGRectGetWidth(self.llSettingView.userTextField.frame)*0.225, CGRectGetMaxY(self.llSettingView.passwordTextField.frame)+CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.2, CGRectGetWidth(self.llSettingView.userTextField.frame)*0.15, CGRectGetWidth(self.llSettingView.userTextField.frame)*0.15);
    [self.llSettingView.registerButton setImage:[UIImage imageNamed:@"adding"] forState:UIControlStateNormal];
    [self.llSettingView.registerButton addTarget:self action:@selector(actionRegisterButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.llSettingView.loginBaseImageView addSubview:self.llSettingView.registerButton];
    
    // 登录
    self.llSettingView.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.llSettingView.loginButton.frame = CGRectMake(CGRectGetMaxX(self.llSettingView.registerButton.frame)+CGRectGetWidth(self.llSettingView.userTextField.frame)*0.05, CGRectGetMinY(self.llSettingView.registerButton.frame), CGRectGetWidth(self.llSettingView.registerButton.frame), CGRectGetHeight(self.llSettingView.registerButton.frame));
    [self.llSettingView.loginButton setImage:[UIImage imageNamed:@"confirming"] forState:UIControlStateNormal];
    [self.llSettingView.loginButton addTarget:self action:@selector(actionLoginButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.llSettingView.loginBaseImageView addSubview:self.llSettingView.loginButton];
    
    // 返回
    self.llSettingView.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.llSettingView.backButton.frame = CGRectMake(CGRectGetMaxX(self.llSettingView.loginButton.frame)+CGRectGetWidth(self.llSettingView.userTextField.frame)*0.05, CGRectGetMinY(self.llSettingView.loginButton.frame), CGRectGetWidth(self.llSettingView.loginButton.frame), CGRectGetHeight(self.llSettingView.loginButton.frame));
    [self.llSettingView.backButton setImage:[UIImage imageNamed:@"canceling"] forState:UIControlStateNormal];
    [self.llSettingView.backButton addTarget:self action:@selector(actionBackButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.llSettingView.loginBaseImageView addSubview:self.llSettingView.backButton];
}
// 登录界面按钮点击方法
- (void)actionRegisterButton:(UIButton *)sender{
    if (!self.llSettingView.registerBaseImageView) {
        [self addRegisterView];
    }
     [UIView animateWithDuration:0.3 animations:^{
        self.llSettingView.registerBaseImageView.frame = CGRectMake(CGRectGetMinX(self.llSettingView.photoImageView.frame), CGRectGetMinY(self.llSettingView.photoImageView.frame), CGRectGetWidth(self.llSettingView.photoImageView.frame), CGRectGetHeight(self.llSettingView.photoImageView.frame));
    }];
    if ([self.llSettingView.registerUserTextField.text isEqualToString:@""] || [self.llSettingView.registerPasswordTextField.text isEqualToString:@""] || [self.llSettingView.registerAgainPasswordTextField.text isEqualToString:@""]){
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"用户名,密码不能为空"];
        [self.llSettingView addSubview:alertView];
        // 如果不为空的话
    } else if(![self.llSettingView.registerPasswordTextField.text isEqualToString:self.llSettingView.registerAgainPasswordTextField.text]){
        // 判断两个密码是不是一样
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"两次输入的密码不一样"];
        [self.llSettingView addSubview:alertView];
        // 输入的正确
    } else if(self.llSettingView.registerPasswordTextField.text.length < 6){
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"密码至少6个字符"];
        [self.llSettingView addSubview:alertView];
    } else {
        [self registerWithUserName:self.llSettingView.registerUserTextField.text UserPassWord:self.llSettingView.registerPasswordTextField.text];
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"注册成功"];
        [self.llSettingView addSubview:alertView];
    }
}

// 注册
- (void)registerWithUserName:(NSString *)name UserPassWord:(NSString *)passWord
{
    AVUser *user = [AVUser user];
    user.username = name;
    user.password = passWord;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
            switch (codeNum) {
                case 202:{
                    WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"用户名已经被占用"];
                    [self.llSettingView addSubview:alertView];
                    break;
                }
                default:
                    break;
            }
        } else {
            WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"登录成功"];
            [self.llSettingView addSubview:alertView];
        }
    }];
}

- (void)actionLoginButton:(UIButton *)sender{
    [AVUser logInWithUsernameInBackground:self.llSettingView.userTextField.text password:self.llSettingView.passwordTextField.text block:^(AVUser *user, NSError *error) {
        if (error) {
            NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
            if (codeNum == 210){
                WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"用户名或者密码错误"];
                [self.llSettingView addSubview:alertView];
            }
        } else {
            WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"登录成功"];
            [self.llSettingView addSubview:alertView];
            
        }
    }];
    
}
- (void)actionBackButton:(UIButton *)sender{
    //NSLog(@"登陆返回");
    
    [UIView animateWithDuration:0.3 animations:^{
        self.llSettingView.loginBaseImageView.frame = CGRectMake(CGRectGetMinX(self.llSettingView.photoImageView.frame), -CGRectGetHeight(self.llSettingView.photoImageView.frame), CGRectGetWidth(self.llSettingView.photoImageView.frame), CGRectGetHeight(self.llSettingView.photoImageView.frame));
    }];
    
}

#pragma mark --- 注册界面布局
- (void)addRegisterView{
    self.llSettingView.registerBaseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.photoImageView.frame), -CGRectGetHeight(self.llSettingView.photoImageView.frame), CGRectGetWidth(self.llSettingView.photoImageView.frame), CGRectGetHeight(self.llSettingView.photoImageView.frame))];
    self.llSettingView.registerBaseImageView.userInteractionEnabled = YES;
    self.llSettingView.registerBaseImageView.image = [UIImage imageNamed:@"whitePaperBG"];
    [self.llSettingView addSubview:self.llSettingView.registerBaseImageView];
    
    // 输入用户名
    self.llSettingView.registerUserTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.llSettingView.photoImageView.frame)*0.1, CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.2, CGRectGetWidth(self.llSettingView.photoImageView.frame)*0.8, CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.12)];
    UIImageView *registerUserLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.llSettingView.registerUserTextField.frame)*0.1, CGRectGetHeight(self.llSettingView.registerUserTextField.frame)*0.8, CGRectGetHeight(self.llSettingView.registerUserTextField.frame)*0.8)];
    registerUserLeft.image = [UIImage imageNamed:@"login_icon"];
    self.llSettingView.registerUserTextField.secureTextEntry = YES;
    self.llSettingView.registerUserTextField.leftView = registerUserLeft;
    self.llSettingView.registerUserTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.llSettingView.registerBaseImageView addSubview:self.llSettingView.registerUserTextField];
    UIView *lineUserView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.registerUserTextField.frame), CGRectGetMaxY(self.llSettingView.registerUserTextField.frame), CGRectGetWidth(self.llSettingView.registerUserTextField.frame), 2)];
    lineUserView.backgroundColor = [UIColor colorWithRed:94/255.0 green:94/255.0 blue:94/255.0 alpha:1.0f];
    [self.llSettingView.registerBaseImageView addSubview:lineUserView];
    
    // 输入密码
    self.llSettingView.registerPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.registerUserTextField.frame), CGRectGetMaxY(self.llSettingView.registerUserTextField.frame)+CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.02, CGRectGetWidth(self.llSettingView.registerUserTextField.frame), CGRectGetHeight(self.llSettingView.registerUserTextField.frame))];
    UIImageView *registerPasswordLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.llSettingView.registerPasswordTextField.frame)*0.1, CGRectGetHeight(self.llSettingView.registerPasswordTextField.frame)*0.8, CGRectGetHeight(self.llSettingView.registerPasswordTextField.frame)*0.8)];
    registerPasswordLeft.image = [UIImage imageNamed:@"password_icon"];
    self.llSettingView.registerPasswordTextField.secureTextEntry = YES;
    self.llSettingView.registerPasswordTextField.leftView = registerPasswordLeft;
    self.llSettingView.registerPasswordTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.llSettingView.registerBaseImageView addSubview:self.llSettingView.registerPasswordTextField];
    UIView *linePasswordView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.registerUserTextField.frame), CGRectGetMaxY(self.llSettingView.registerPasswordTextField.frame), CGRectGetWidth(self.llSettingView.registerPasswordTextField.frame), 2)];
    linePasswordView.backgroundColor = [UIColor colorWithRed:94/255.0 green:94/255.0 blue:94/255.0 alpha:1.0f];
    [self.llSettingView.registerBaseImageView addSubview:linePasswordView];
    
    
    // 再次输入密码
    self.llSettingView.registerAgainPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.registerPasswordTextField.frame), CGRectGetMaxY(self.llSettingView.registerPasswordTextField.frame)+CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.02, CGRectGetWidth(self.llSettingView.registerPasswordTextField.frame), CGRectGetHeight(self.llSettingView.registerPasswordTextField.frame))];
    UIImageView *againPasswordLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.llSettingView.registerAgainPasswordTextField.frame)*0.1, CGRectGetHeight(self.llSettingView.registerAgainPasswordTextField.frame)*0.8, CGRectGetHeight(self.llSettingView.registerAgainPasswordTextField.frame)*0.8)];
    againPasswordLeft.image = [UIImage imageNamed:@"password_icon"];
    self.llSettingView.registerAgainPasswordTextField.secureTextEntry = YES;
    self.llSettingView.registerAgainPasswordTextField.leftView = againPasswordLeft;
    self.llSettingView.registerAgainPasswordTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.llSettingView.registerBaseImageView addSubview:self.llSettingView.registerAgainPasswordTextField];
    UIView *lineAgainPasswordView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.llSettingView.registerAgainPasswordTextField.frame), CGRectGetMaxY(self.llSettingView.registerAgainPasswordTextField.frame), CGRectGetWidth(self.llSettingView.registerAgainPasswordTextField.frame), 2)];
    lineAgainPasswordView.backgroundColor = [UIColor colorWithRed:94/255.0 green:94/255.0 blue:94/255.0 alpha:1.0f];
    [self.llSettingView.registerBaseImageView addSubview:lineAgainPasswordView];
    
    // 返回
    self.llSettingView.registerBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.llSettingView.registerBackButton.frame = CGRectMake(CGRectGetMinX(self.llSettingView.registerAgainPasswordTextField.frame)+CGRectGetWidth(self.llSettingView.registerAgainPasswordTextField.frame)*0.325, CGRectGetMaxY(self.llSettingView.registerAgainPasswordTextField.frame)+CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.1, CGRectGetWidth(self.llSettingView.photoImageView.frame)*0.12, CGRectGetHeight(self.llSettingView.photoImageView.frame)*0.12);
    [self.llSettingView.registerBackButton setImage:[UIImage imageNamed:@"canceling"] forState:UIControlStateNormal];
    [self.llSettingView.registerBackButton addTarget:self action:@selector(actionRegisterBackButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.llSettingView.registerBaseImageView addSubview:self.llSettingView.registerBackButton];
    
    // 注册
    self.llSettingView.registerAchieveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.llSettingView.registerAchieveButton.frame = CGRectMake(CGRectGetMaxX(self.llSettingView.registerBackButton.frame)+CGRectGetWidth(self.llSettingView.photoImageView.frame)*0.05, CGRectGetMinY(self.llSettingView.registerBackButton.frame), CGRectGetWidth(self.llSettingView.registerBackButton.frame), CGRectGetHeight(self.llSettingView.registerBackButton.frame));
    [self.llSettingView.registerAchieveButton setImage:[UIImage imageNamed:@"confirming"] forState:UIControlStateNormal];
//    [self.llSettingView.registerAchieveButton addTarget:self action:@selector(actionRegisterAchieveButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.llSettingView.registerBaseImageView addSubview:self.llSettingView.registerAchieveButton];
    
    
}

// 注册界面按钮点击方法
- (void)actionRegisterBackButton:(UIButton *)sender{
    //NSLog(@"注册返回");
    
    [UIView animateWithDuration:0.3 animations:^{
        self.llSettingView.registerBaseImageView.frame = CGRectMake(CGRectGetMinX(self.llSettingView.photoImageView.frame), -CGRectGetHeight(self.llSettingView.photoImageView.frame), CGRectGetWidth(self.llSettingView.photoImageView.frame), CGRectGetHeight(self.llSettingView.photoImageView.frame));
    }];
}



#pragma mark --- 从相册添加用户图像
- (void)actionPhotoTap:(UITapGestureRecognizer *)sender{
    // 判断系统是否有相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        // 取得照片之后是否允许进行编辑
        picker.allowsEditing = YES;
        // 指定使用系统相册
//        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 模态进入系统相册
        [self presentViewController:picker animated:YES completion:nil];
    }
}
// 代理方法 - 拍摄完成或者选择相册完成之后调用的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    self.llSettingView.photoImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    AVUser *user = [AVUser currentUser];
    NSData *imageData = UIImagePNGRepresentation(self.llSettingView.photoImageView.image);
    AVFile *imageFile = [AVFile fileWithData:imageData];
    [user setObject:imageFile forKey:@"photo"];
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            //NSLog(@"上传头像成功");
        } else {
            //NSLog(@"上传头像失败");
        }
    }];
    
    // 模态返回
    [self dismissViewControllerAnimated:YES completion:nil];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
